package com.sike.netty.jinjie.bytebuf.recycler;

import io.netty.util.Recycler;

public class RecyclerTest {

    private static final Recycler<User> USER_RECYCLER = new Recycler<User>() {
        @Override
        protected User newObject(Handle<User> handle) {
            return new User(handle);
        }
    };

    public static void main(String[] args) {
        // 从对象池中获取对象
        User user1 = USER_RECYCLER.get();
        System.out.println(user1);

        // 从对象池中获取对象
        User user2 = USER_RECYCLER.get();
        System.out.println(user2);

        // 回收对象 user1
        user1.getHandler().recycle(user1);
        user1.getHandler().recycle(user1);

        // 再次对象池中获取对象
        User user3 = USER_RECYCLER.get();
        System.out.println(user3);

        System.out.println("user1 == user3：" + (user1 == user3));
    }
}
